import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { query } from '/@/utils/params';
// import { Account } from '../../system/account/account';

export interface DealerVo {
	id?: string;
	orgId?: string;
	orgName: string;
	orgAddress: string;
	orgContact: string;
	userName: string;
	userTel: string;
	account: string; // 账号
	password?: string; // 密码
	parentOrgId?: string;
	status: string;
	regions?: string[][];
	regionsName?: string;
	createdAt?: string;
	deviceCount?: number;
	activeCount?: number;
}
export interface DealerDTO {
	orgName: string; // 经销商名称
	contact: string; // 联系电话
	address: string; // 地址
	regions: string;
	createdAt: string;
}
import { useDivision } from './division';
import { buildTree, filterRegion, getRegionNames, parseRegionToJson } from '/@/utils/division';
import { easyDate } from '/@/utils/formatTime';
import { useUserInfo } from '/@/stores/userInfo';
export const useDealer = defineStore('dealer', {
	actions: {
		async getDealerList(name: string, pageSize: number, pageNumber: number) {
			await useDivision().getDivisions();
			return service.get(query('/api/dealer', { name, pageSize, pageNumber })).then(async (res) => {
				const list = res.data.list.map((c: any): DealerVo => {
					console.log('------', c.org.regions);
					const regions = parseRegionToJson(c.org.regions);
					return {
						id: c.id,
						orgId: c.orgId,
						orgName: c.org.orgName,
						orgAddress: c.org.address,
						orgContact: c.org.contact,
						userName: c.userInfo.name,
						userTel: c.phone,
						account: c.account,
						status: c.status,
						parentOrgId: c.org.parentId,
						deviceCount: c.deviceCount,
						activeCount: c.activeCount,
						regions: regions,
						createdAt: easyDate(c.createdAt),
						regionsName: regions ? getRegionNames(useDivision().divisions, regions) : '',
					};
				});
				const total = res.data.total;
				return {
					list,
					total,
				}
			});
		},
		async updateRegions(orgId: string, regions: string) {
			return service.patch(`/api/dealer/${orgId}/regions`, { regions });
		},

		async getDevices(orgId: string) {
			return service.get(`/api/dealer/${orgId}/devices`);
		},

		async saveDealer(vo: Partial<DealerVo>) {
			return service.post('/api/dealer/create', { ...vo, regions: JSON.stringify(vo.regions) });
		},
		/**
		 * 编辑
		 */
		async updateDealer(vo: Partial<DealerVo>) {
			return service.patch(`/api/dealer/${vo.id}`, { ...vo, regions: JSON.stringify(vo.regions) });
		},

		async delete(id: string) {
			return service.delete(`/api/dealer/${id}`);
		},
		async toggoleStatus(id: string, status: number) {
			return service.patch(`/api/dealer/${id}/status`, { status });
		},
		async getOrgInfos(orgId: string) {
			return service.get(`/api/dealer/orgInfos?orgId=${orgId}`);
		},
		async getDivision() {
			return useDivision()
				.getDivisions()
				.then((res) => {
					const userInfo = useUserInfo().userInfo;

					if (userInfo?.orgType === 'DEALER') {
						return this.getOrgInfos(userInfo!.orgId!).then((res1) => {
							const regions = res1.data.regions
							console.log('regions=======', regions);
							const regionCodes = parseRegionToJson(regions);
							if (regionCodes) {
								return buildTree(filterRegion(res, regionCodes));
							} else {
								return [];
							}
						})
					}
					console.log('res', res);
					return buildTree(res);
				});
		},
	},
});
